package com.example.blog;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@MapperScan("com.example.blog.mapper")
public class BlogApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
    
    @Bean
    public CommandLineRunner createUploadDirectories() {
        return args -> {
            try {
                // 确保上传目录存在
                String basePath = System.getProperty("user.dir");
                Path uploadPath = Paths.get(basePath, "uploads", "images");
                if (!Files.exists(uploadPath)) {
                    Files.createDirectories(uploadPath);
                    System.out.println("已创建图片上传目录: " + uploadPath);
                } else {
                    System.out.println("图片上传目录已存在: " + uploadPath);
                }
            } catch (Exception e) {
                System.err.println("创建上传目录失败: " + e.getMessage());
            }
        };
    }
}
